package com.android.picotts.ui.activity.pick;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.picotts.R;
import com.android.picotts.ui.activity.common.ArrayListAdapter;
import com.android.picotts.ui.activity.pick.AppListAdapter.AppInfo;

public class AppListAdapter extends ArrayListAdapter<AppInfo> implements OnClickListener {
    private Context mcontext = null;
    private ConcurrentHashMap<Integer, Integer> chkMaps = new ConcurrentHashMap<Integer, Integer>();
    private boolean isMutilable = false;
    
    public AppListAdapter(Context context) {
        this(context, true);
    }
    
    public AppListAdapter(Context context, boolean isMutilable) {
        super(context);
        mcontext = context;
        this.isMutilable = isMutilable;
    }
    
    public List<AppInfo> getChecks() {
        if(chkMaps.isEmpty()) {
            return null;
        }
        List<AppInfo> list = new ArrayList<AppListAdapter.AppInfo>();
        Iterator<Integer> itor = chkMaps.values().iterator();
        while(itor.hasNext()) {
            list.add(getItem( itor.next()));
        }
        return list;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListHolder mListHolder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(mcontext).inflate(R.layout.listitem_pick, null);
            mListHolder = new ListHolder();
            mListHolder.mfileIcon = (ImageView) convertView.findViewById(R.id.icon);
            mListHolder.mfileName = (TextView) convertView.findViewById(R.id.name);
            mListHolder.mfileSize = (TextView) convertView.findViewById(R.id.size);
            mListHolder.mfileTime = (TextView) convertView.findViewById(R.id.title);
            mListHolder.mChkBox =  (CheckBox) convertView.findViewById(R.id.checkbox);
            
            mListHolder.mChkBox.setOnClickListener(this);
            if(isMutilable) {
                mListHolder.mChkBox.setButtonDrawable(R.drawable.checkbox_gou);
            }
            convertView.setTag(mListHolder);
        } else {
            mListHolder = (ListHolder) convertView.getTag();
        }
        
        AppInfo f = this.getItem(position);
        if (f != null) {
            mListHolder.mfileIcon.setImageDrawable(f.appIcon);
            mListHolder.mfileName.setText(f.appName);
            mListHolder.mfileTime.setText(f.versionName);
        }        
        mListHolder.mChkBox.setChecked(chkMaps.containsKey(position));
        mListHolder.mChkBox.setTag(position);
        return convertView;
    }
    
    @Override
    public void onClick(View v) {
        int pos = (Integer)v.getTag();
        if(chkMaps.containsKey(pos)) {
            chkMaps.remove(pos);
        }else {
            if(!isMutilable) {
                chkMaps.clear();
            }
            chkMaps.put(pos, pos);
        }
        notifyDataSetChanged();
    }

    static class ListHolder {
        ImageView mfileIcon;
        TextView mfileName;
        TextView mfileSize;
        TextView mfileTime;
        CheckBox mChkBox;
    }
    
    public static class AppInfo {  
        public String appName="";
        public String packageName="";
        public String versionName="";
        public Drawable appIcon=null;
    }
}